/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2019年6月22日
 * <修改描述:>
 */
package com.tx.component.social.script;

import com.tx.core.TxConstants;
import com.tx.core.dbscript.initializer.AbstractTableInitializer;
import com.tx.core.ddlutil.builder.DDLBuilder;
import com.tx.core.ddlutil.builder.alter.AlterTableDDLBuilder;
import com.tx.core.ddlutil.builder.create.CreateTableDDLBuilder;
import com.tx.core.ddlutil.executor.TableDDLExecutor;

/**
 * 安全容器表初始化器<br/>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2019年6月22日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class SocialContextTableInitializer extends AbstractTableInitializer {
    
    /** <默认构造函数> */
    public SocialContextTableInitializer() {
        super();
    }
    
    /**
     * 
     */
    @Override
    public String tables(TableDDLExecutor tableDDLExecutor,
            boolean tableAutoInitialize) {
        //初始化表定义
        StringBuilder sb = new StringBuilder(TxConstants.INITIAL_STR_LENGTH);
        
        //初始化表定义
        sb.append(COMMENT_PREFIX)
                .append("----------table:table_sec_social_account----------")
                .append(COMMENT_SUFFIX)
                .append(LINE_SEPARATOR);
        String secRoleType = table_sec_social_account(tableDDLExecutor,
                tableAutoInitialize);
        sb.append(secRoleType);
        sb.append(LINE_SEPARATOR);
        
        return sb.toString();
    }
    
    /**
     * 权限项<br/>
     * <功能详细描述> [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    private String table_sec_social_account(TableDDLExecutor tableDDLExecutor,
            boolean tableAutoInitialize) {
        String tableName = "sec_social_account";
        
        CreateTableDDLBuilder createDDLBuilder = null;
        AlterTableDDLBuilder alterDDLBuilder = null;
        DDLBuilder<?> ddlBuilder = null;
        
        if (tableDDLExecutor.exists(tableName)) {
            alterDDLBuilder = tableDDLExecutor
                    .generateAlterTableDDLBuilder(tableName);
            ddlBuilder = alterDDLBuilder;
        } else {
            createDDLBuilder = tableDDLExecutor
                    .generateCreateTableDDLBuilder(tableName);
            ddlBuilder = createDDLBuilder;
        }
        
        sec_social_account(ddlBuilder);//写入表结构
        
        if (alterDDLBuilder != null
                && alterDDLBuilder.compare().isNeedAlter()) {
            if (tableAutoInitialize) {
                tableDDLExecutor.alter(alterDDLBuilder);
            }
            return alterDDLBuilder.alterSql();
        } else if (createDDLBuilder != null) {
            if (tableAutoInitialize) {
                tableDDLExecutor.create(createDDLBuilder);
            }
            return createDDLBuilder.createSql();
        }
        return "";
    }
    
    /**
     * 权限项<br/>
     * <功能详细描述>
     * @param ddlBuilder [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public void sec_social_account(DDLBuilder<?> ddlBuilder) {
        /*
            drop table if exists SEC_SOCIAL_ACCOUNT;
            create table SEC_SOCIAL_ACCOUNT(
                id varchar(64) not null,
                uniqueId varchar(64) not null,
                type varchar(64) not null,
                userId varchar(64) not null,
                userType varchar(64) not null,
                socialUsername varchar(128),
                headImgUrl varchar(256),
                attributes varchar(1000),
                lastUpdateDate datetime(6) not null default now(6),
                createDate datetime(6) not null default now(6),
                primary key(id)
            );
            create unique index IDX_SEC_SOCIAL_ACCOUNT_00 on SEC_SOCIAL_ACCOUNT(uniqueId,type,userType);
            create unique index IDX_SEC_SOCIAL_ACCOUNT_01 on SEC_SOCIAL_ACCOUNT(userId,userType,type);
         */
        ddlBuilder.newColumnOfVarchar(true, "id", 64, true, null)
                .newColumnOfVarchar("uniqueId", 64, true, null)
                .newColumnOfVarchar("type", 64, true, null)
                .newColumnOfVarchar("userId", 64, true, null)
                .newColumnOfVarchar("userType", 64, true, null)
                .newColumnOfVarchar("socialUsername", 128, false, null)
                .newColumnOfVarchar("headImgUrl", 256, false, null)
                .newColumnOfVarchar("attributes", 1000, false, null)
                .newColumnOfDate("lastUpdateDate", true, true)
                .newColumnOfDate("createDate", true, true);
        ddlBuilder.newIndex(true,
                "IDX_SEC_SOCIAL_ACCOUNT_00",
                "uniqueId,type,userType");
        ddlBuilder.newIndex(true,
                "IDX_SEC_SOCIAL_ACCOUNT_01",
                "userId,userType,type");
    }
    
}
